home *** CD-ROM | disk | FTP | other *** search
/ Just Call Me Internet / Just Call Me Internet.iso / prog / mint / duftp / gui2ftp.c < prev    next >
C/C++ Source or Header  |  1995-06-17  |  10KB  |  333 lines

  1. /*
  2.     DUFTP
  3. */
  4.  
  5. // GUI links to FTP
  6.  
  7. #include <DULIB.H>
  8. #include <UNISTD.H>
  9. #include <FCNTL.H>
  10. #include "init.h"
  11. #include "globals.h"
  12. #include "servers.h"
  13. #include "ftp.h"
  14. #include "duftp_n.h"
  15.  
  16. short open_selected_server(void)
  17. {
  18.     OBJECT *oi;
  19.     ICONBLK *ib;
  20.     FILE *f;
  21.     short n;
  22.     char *d;
  23.     
  24.     set_dialog_text(Ftp_progress, Ftp_operation, "Opening FTP link.");
  25.     set_dialog_text(Ftp_progress, Ftp_status, "Initialising.");
  26.     activate_dialog(Ftp_progress, "DUftp Accessing Remote", DIAL_NO_CLOSE);
  27.  
  28. // Chop all login/ip info's at first space, as you cann't have spaces in them anyway
  29.     for(n=0; (selected_server->ip_name[n]!=' ')&&(selected_server->ip_name[n]!='\0'); n++);
  30.     selected_server->ip_name[n]='\0';
  31.     for(n=0; (selected_server->login[n]!=' ')&&(selected_server->password[n]!='\0'); n++);
  32.     selected_server->login[n]='\0';
  33.     for(n=0; (selected_server->password[n]!=' ')&&(selected_server->password[n]!='\0'); n++);
  34.     selected_server->password[n]='\0';
  35.  
  36.     connect_to_server(selected_server->ip_name);        // Actually connect to the server
  37.  
  38.     if (connected)
  39.         login(selected_server->login, selected_server->password);    // Login to the server
  40.  
  41.     sprintf(new_server_address,"%s",selected_server->ip_name);
  42.     sprintf(new_server_login,"%s",selected_server->login);
  43.     sprintf(new_server_password,"%s",selected_server->password);
  44.     sprintf(new_server_path,"%s",selected_server->path);
  45.  
  46.     rsrc_gaddr(0,Mainwindow,&oi);
  47.     ib=(ICONBLK*)(oi+Online_offline)->ob_spec;
  48.  
  49.     if (connected)                                        // Update the gnatty on/offline icon.
  50.     {
  51.     
  52. // Change remote directory
  53.         if (selected_server->path[0]!=' ')
  54.         {
  55.             set_dialog_text(Ftp_progress, Ftp_operation, "Setting initial directory.");
  56.             set_dialog_text(Ftp_progress, Ftp_status, "Calling remote_cd.");
  57.             remote_cd(selected_server->path);
  58.         }
  59.         
  60.         d=remote_pwd();
  61.         if (d)                            // If we can get the current remote directory, use it
  62.             sprintf(new_server_path,"%s",d);
  63.         else                            // Otherwise, just use the initial directory.
  64.             sprintf(new_server_path,"/");
  65.  
  66.         set_dialog_text(Mainwindow, Remote_path, new_server_path);
  67.  
  68.         chdir(initial_dir);
  69.         f=fopen("temp.fil","w");
  70.         if(f) fclose(f);
  71.  
  72.         remote_ls("temp.fil",NULL);
  73.  
  74.         ib->ib_ptext="Online";
  75.         (oi+Online_offline)->ob_state|=SELECTED;
  76.         (oi+Open_server)->ob_state|=DISABLED;
  77.         (oi+Open_new_server)->ob_state|=DISABLED;
  78.         (oi+This_server)->ob_state&=~SHADOWED;
  79.         (oi+This_server)->ob_state|=DISABLED;
  80.         (oi+This_server)->ob_flags&=~SELECTABLE;
  81.  
  82.         if (remote_directory) dispose_file_list(remote_directory);
  83.         remote_directory=ls_to_list("temp.fil");
  84.         if (remote_files) free(remote_files);
  85.         remote_files=extract_filenames(remote_directory);
  86.         Change_scroll_list(Mainwindow, Remote_fs, remote_files, file_count+1);
  87.     }else{
  88.         ib->ib_ptext="Unconnected";
  89.         (oi+Online_offline)->ob_state&=~SELECTED;
  90.         (oi+Open_server)->ob_state&=~DISABLED;
  91.         (oi+Open_new_server)->ob_state&=~DISABLED;
  92.         (oi+This_server)->ob_state|=SHADOWED;
  93.         (oi+This_server)->ob_state&=~DISABLED;
  94.         (oi+This_server)->ob_flags|=SELECTABLE;
  95.     }
  96.  
  97.     close_dialog(Ftp_progress);
  98.     dialog_update(Mainwindow);
  99.     
  100.     return TRUE;
  101. }
  102.  
  103. short open_new_server(void)
  104. {
  105.     OBJECT *oi;
  106.     ICONBLK *ib;
  107.     FILE *f;
  108.     short n;
  109.     char *d;
  110.     
  111.     set_dialog_text(Ftp_progress, Ftp_operation, "Opening new FTP link.");
  112.     set_dialog_text(Ftp_progress, Ftp_status, "Initialising.");
  113.     activate_dialog(Ftp_progress, "DUftp Accessing Remote", DIAL_NO_CLOSE);
  114.  
  115.     for(n=0; new_server_address[n]!=' '; n++);
  116.     new_server_address[n]='\0';
  117.     for(n=0; new_server_login[n]!=' '; n++);
  118.     new_server_login[n]='\0';
  119.     for(n=0; new_server_password[n]!=' '; n++);
  120.     new_server_password[n]='\0';
  121.     for(n=0; new_server_path[n]!=' '; n++);
  122.     new_server_path[n]='\0';
  123.  
  124.     connect_to_server(new_server_address);            // Actually connect to the server
  125.  
  126.     if (connected)
  127.         login(new_server_login, new_server_password);    // Login to the server
  128.  
  129.     rsrc_gaddr(0,Mainwindow,&oi);
  130.     ib=(ICONBLK*)(oi+Online_offline)->ob_spec;
  131.  
  132.     if (connected)                                        // Update the gnatty on/offline icon.
  133.     {
  134.     
  135. // Change remote directory
  136.         if (new_server_path[0]!=' ')
  137.         {
  138.             set_dialog_text(Ftp_progress, Ftp_operation, "Setting initial directory.");
  139.             set_dialog_text(Ftp_progress, Ftp_status, "Calling remote_cd.");
  140.             remote_cd(new_server_path);
  141.         }
  142.  
  143.         d=remote_pwd();
  144.         if (d)                            // If we can get the current remote directory, use it
  145.             sprintf(new_server_path,"%s",d);
  146.         else                            // Otherwise, just use the initial directory.
  147.             sprintf(new_server_path,"/");
  148.  
  149.         set_dialog_text(Mainwindow, Remote_path, new_server_path);
  150.  
  151.         chdir(initial_dir);
  152.         f=fopen("temp.fil","w");
  153.         if(f) fclose(f);
  154.  
  155.         remote_ls("temp.fil",NULL);
  156.  
  157.         ib->ib_ptext="Online";
  158.         (oi+Online_offline)->ob_state|=SELECTED;
  159.         (oi+Open_server)->ob_state|=DISABLED;
  160.         (oi+Open_new_server)->ob_state|=DISABLED;
  161.         (oi+This_server)->ob_state&=~SHADOWED;
  162.         (oi+This_server)->ob_state|=DISABLED;
  163.         (oi+This_server)->ob_flags&=~SELECTABLE;
  164.  
  165.         if (remote_directory) dispose_file_list(remote_directory);
  166.         remote_directory=ls_to_list("temp.fil");
  167.         if (remote_files) free(remote_files);
  168.         remote_files=extract_filenames(remote_directory);
  169.         Change_scroll_list(Mainwindow, Remote_fs, remote_files, file_count+1);
  170.     }else{
  171.         ib->ib_ptext="Unconnected";
  172.         (oi+Online_offline)->ob_state&=~SELECTED;
  173.         (oi+Open_server)->ob_state&=~DISABLED;
  174.         (oi+Open_new_server)->ob_state&=~DISABLED;
  175.         (oi+This_server)->ob_state|=SHADOWED;
  176.         (oi+This_server)->ob_state&=~DISABLED;
  177.         (oi+This_server)->ob_flags|=SELECTABLE;
  178.     }
  179.  
  180.     close_dialog(Ftp_progress);
  181.     dialog_update(Mainwindow);
  182.     
  183.     return TRUE;
  184. }
  185.  
  186. short close_current_session(void)
  187. {
  188.     OBJECT *oi;
  189.     ICONBLK *ib;
  190.     
  191.     disconnect();
  192.  
  193.     rsrc_gaddr(0,Mainwindow,&oi);
  194.     ib=(ICONBLK*)(oi+Online_offline)->ob_spec;
  195.  
  196.     if (connected)                                        // Update the gnatty on/offline icon.
  197.     {
  198.         ib->ib_ptext="Online";
  199.         (oi+Online_offline)->ob_state|=SELECTED;
  200.         (oi+Open_server)->ob_state|=DISABLED;
  201.         (oi+Open_new_server)->ob_state|=DISABLED;
  202.         (oi+This_server)->ob_state&=~SHADOWED;
  203.         (oi+This_server)->ob_state|=DISABLED;
  204.         (oi+This_server)->ob_flags&=~SELECTABLE;
  205.     }else{
  206.         ib->ib_ptext="Unconnected";
  207.         (oi+Online_offline)->ob_state&=~SELECTED;
  208.         (oi+Open_server)->ob_state&=~DISABLED;
  209.         (oi+Open_new_server)->ob_state&=~DISABLED;
  210.         (oi+This_server)->ob_state|=SHADOWED;
  211.         (oi+This_server)->ob_state&=~DISABLED;
  212.         (oi+This_server)->ob_flags|=SELECTABLE;
  213.     }
  214.  
  215.     reset_remote_list();
  216.     
  217.     dialog_update(Mainwindow);
  218.     
  219.     return TRUE;
  220. }
  221.  
  222. // Drag & Drop a filename from Remote_fs to Local_fs
  223. //  If successful, FTP the file from the remote server.
  224. void get_file(char *filename)
  225. {
  226.     FILE *f;
  227.     OBJECT *TheDial;
  228.     char alert[200];
  229.     short ob,junk,mx,my,mb;
  230.     short w,pt[8],tw,th;
  231.     
  232.     if (!connected) return;
  233.  
  234.     vqt_extent(x_handle, filename, pt);
  235.     tw=pt[2]-pt[0];
  236.     th=pt[1]-pt[7];
  237.     
  238.     graf_mkstate(&mx,&my,&mb,&junk);
  239.     if (!mb) return;
  240.     
  241.     wind_update(BEG_MCTRL);                    // Drag & drop the filename
  242.     graf_mouse(FLAT_HAND,NULL);
  243.  
  244.     graf_dragbox(tw,th,mx,my,scrn_x,scrn_y,scrn_w,scrn_h,&mx,&my);
  245.         
  246.     graf_mouse(ARROW,NULL);
  247.     wind_update(END_MCTRL);
  248.  
  249.     w=wind_find(mx,my);
  250.     
  251.     if (w)
  252.     {
  253.         if (windows[w].the_dialog==Mainwindow)
  254.         {
  255.             rsrc_gaddr(0,Mainwindow,&TheDial);
  256.             ob=objc_find(TheDial, 0, 5, mx, my);        // Find the object we dropped filename on
  257.  
  258.             if (ob==Local_fs+scroll_text_area)            // If it was Local_fs, then do an FTP get for it
  259.             {
  260.                 sprintf(alert,"[1][ Get remote file :             | '%s'. ][ Ok | Cancel ]",filename);
  261.                 if (form_alert(1,alert)==2) return;
  262.                 
  263.                 chdir(current_local_path);                // Shift to the current directory
  264.                 f=fopen(filename,"w");                    // Create the file
  265.                 if (f)
  266.                 {
  267.                     fclose(f);
  268.                 }else{
  269.                     form_alert(1,"[3][ Cann't create local file. ][ Cancel ]");
  270.                     return;
  271.                 }
  272.  
  273.                 set_dialog_text(Ftp_progress, Ftp_operation, "Getting file from remote.");
  274.                 set_dialog_text(Ftp_progress, Ftp_status, "Initialising.");
  275.                 activate_dialog(Ftp_progress, "DUftp Accessing Remote", DIAL_NO_CLOSE);
  276.                 
  277.                 remote_get(filename,filename);            // Fetch the file
  278.  
  279.                 close_dialog(Ftp_progress);
  280.                 
  281.                 get_local_files();                        // Update the local file list
  282.                 if (local_files) free(local_files);
  283.                 th=file_count;
  284.                 local_files=extract_filenames(local_directory);
  285.                 local_file_count=file_count;
  286.                 file_count=th;
  287.                 Change_scroll_list(Mainwindow, Local_fs, local_files, local_file_count+1);
  288.             }
  289.         }
  290.     }
  291. }
  292.  
  293. // Change the current remote directory, and get a new file list.
  294. void change_remote_directory(char *dir_name)
  295. {
  296.     FILE *f;
  297.     char *d;
  298.  
  299.     if (!connected) return;
  300.     
  301. // Open remote progress window
  302.     set_dialog_text(Ftp_progress, Ftp_operation, "Changing remote directory.");
  303.     set_dialog_text(Ftp_progress, Ftp_status, "Calling remote_cd.");
  304.     activate_dialog(Ftp_progress, "DUftp Accessing Remote", DIAL_NO_CLOSE);
  305.  
  306. // Change remote directory
  307.     remote_cd(dir_name);        
  308.  
  309. // Check where we ended up
  310.     d=remote_pwd();
  311.     if (d)                            // If we can get the current remote directory,     use it
  312.         sprintf(new_server_path,"%s",d);
  313.     else                            // Otherwise, just use the initial directory.
  314.         sprintf(new_server_path,"/");
  315.  
  316.     set_dialog_text(Mainwindow, Remote_path, new_server_path);
  317.  
  318. // Request a new file list
  319.     chdir(initial_dir);
  320.     f=fopen("temp.fil","w");
  321.     if(f) fclose(f);
  322.     remote_ls("temp.fil",NULL);    
  323.     if (remote_directory) dispose_file_list(remote_directory);
  324.     remote_directory=ls_to_list("temp.fil");
  325.     if (remote_files) free(remote_files);
  326.     remote_files=extract_filenames(remote_directory);
  327.     Change_scroll_list(Mainwindow, Remote_fs, remote_files, file_count+1);
  328.  
  329. // Close remote progress & update main window
  330.     close_dialog(Ftp_progress);
  331.     dialog_update(Mainwindow);    
  332. }
  333.